/**
 * Created by john on 2018/3/28.
 */
$(function () {
   // "text!com/contact.ejs"
   require(["text!pcCom/tpl/tree.ejs"],function (html) {
      $('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', '关闭');

      var $wrap = $(".wrap");

      var tools = $(".operate .tools");
      //工具栏
      tools.on("click",function () {
         var edit = $(".tree .edit");
         if(edit.is(":visible")) {
            edit.hide('fast');
            tools.data("hide",true);
         } else {
            edit.show('fast');
            tools.data("hide",false);
         }
      });

      //打印
      $(".print").on("click",function () {
         $(".tree").print({
            globalStyles:true,//是否包含父文档的样式，默认为true
            mediaPrint:false,//是否包含media='print'的链接标签。会被globalStyles选项覆盖，默认为false
            stylesheet:null,//外部样式表的URL地址，默认为null
            noPrintSelector:".no-print",//不想打印的元素的jQuery选择器，默认为".no-print"
            iframe:true,//是否使用一个iframe来替代打印表单的弹出窗口，true为在本页面进行打印，false就是说新开一个页面打印，默认为true
            append:null,//将内容添加到打印内容的后面
            prepend:null,//将内容添加到打印内容的前面，可以用来作为要打印内容
            deferred: $.Deferred()//回调函数
         });
      });
      
      //展开
      $(".open").on("click",function () {
         $('.tree li.parent_li > span').each(function (index,ele) {
            var me = $(ele);
            var parent = me.parent('li.parent_li');
            var Oul = parent.find(' > ul');
            if(Oul.length > 0){
               var children = Oul.find('>li');
               if(!children.is(":visible")) {

                  children.show('fast');
                  me.attr('title', '关闭').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');

               }
            }else{

               //需要获取子节点
               getChilds(me,parent);
            }
         });
      });

      bindEvent();
      
      //绑定事件
      function  bindEvent() {
         $('.tree li.parent_li > span').off('click', slideTree);
         $(".tree i.changeF").off("click",changeF);
         $(".tree i.changeR").off("click",changeR);

         $('.tree li.parent_li > span').on('click', slideTree);
         $(".tree i.changeF").on("click",changeF);
         $(".tree i.changeR").on("click",changeR);
      }

      function changeF(e) {
         var me = $(this);
         $wrap.removeClass("hide");
         $wrap.currDom = me;
         e.stopPropagation();
      }
      
      function changeR(e) {
         var me = $(this);
         var item = me.data("item");
         $.ajax({
            type:"post",
            url:"/api/remove",
            data:{
               _id:item._id,
               parent:item.parent,
               child:JSON.stringify(item.child)
            },
            success:function (data) {
              window.location.reload();
            }
         })
         e.stopPropagation();
      }
      

      function slideTree(e) {
         var me =  $(this);
         var parent = me.parent('li.parent_li');
         var Oul = parent.find(' > ul');
         if(Oul.length > 0){
            var children = Oul.find('>li');
            if(children.is(":visible")) {
               children.hide('fast');
               me.attr('title', '展开').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');
            } else {
               children.show('fast');
               me.attr('title', '关闭').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');

            }
         }else{

            //需要获取子节点
            getChilds(me,parent);
         }

         e.stopPropagation();
      }

      function  getChilds(me,parent) {
         if(me.data("loading")){
            return ;
         }

         me.data("loading",true);
         var childs = parent.data("childs") || [];

         if(childs.length > 0){
            $.ajax({
               type:"post",
               url:"/api/child",
               data:{
                  ids:JSON.stringify(childs)
               },
               success:function (data) {
                  var hide =  tools.data("hide");
                  data.hide = hide;
                  var str = ejs.render(html,data || {});
                  parent.append(str);
                  me.attr('title', '关闭').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');
                  bindEvent();
               },
               complete:function () {
                  me.data("loading",false);
               }
            })
         }

      }


      $wrap.on("click",".cancel",function () {
         $wrap.addClass("hide");
      });

      $wrap.on("click",".confirm",function () {
         $wrap.addClass("hide");
         var dom = $wrap.currDom;
         var parent = $wrap.find(".pid").val();
         if(dom){
            var _id = dom.data('id');
            var old = dom.data("parent") || '-1';
             $.ajax({
                type:'get',
                url:'/api/change/parent',
                data:{
                   _id:_id,
                   old:old,
                   parent:parent || -1,
                },
                success:function (res) {
                   console.log(res);
                   alert("修改成功.");
                   setTimeout(function () {
                      window.location.reload();
                   },1000);
                }
             })
         }

      });
      
      $("#loginout").on("click",function () {
         $.ajax({
            type:"post",
            url:"/api/loginout",
            success:function (res) {
               window.location.reload();
            }
         })
      });

      
   });
});